//
//  AppDelegate+ConfigLogger.m
//  LoggerTest
//
//  Created by Mr.X on 16/2/17.
//  Copyright © 2016年 Mr.X. All rights reserved.
//

#import "AppDelegate+ConfigLogger.h"

@implementation AppDelegate (ConfigLogger)


//Lumberjack DDLog的配置
- (void)configLogger{
    
    //配置自定义Log方式
    [[DDTTYLogger sharedInstance] setLogFormatter:self];
    
    //启动日志颜色区分
    [[DDTTYLogger sharedInstance] setColorsEnabled:YES];
    //添加控制台日志打印输出
    [DDLog addLogger:[DDTTYLogger sharedInstance]];
    
    //初始化文件打印输出类
    DDFileLogger *fileLogger = [[DDFileLogger alloc] init];
    // 一个LogFile的有效期长，有效期内Log都会写入该LogFile
    fileLogger.rollingFrequency = 60 * 60 * 24;
    //最多LogFile的数量
    fileLogger.logFileManager.maximumNumberOfLogFiles = 7;
    //添加文件日志打印输出
    [DDLog addLogger:fileLogger];
    
    //设置自定义打印颜色，ForegroundColor为打印字体前景色，backgroundColor为打印行背景色，flag为打印的日志种类
    [[DDTTYLogger sharedInstance] setForegroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0] backgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0] forFlag:DDLogFlagInfo];
    [[DDTTYLogger sharedInstance] setForegroundColor:[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0] backgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0] forFlag:DDLogFlagWarning];
}

/**
 *  @author X
 *
 *  实现
 *
 *  @param logMessage 打印信息类
 *
 *  @return 自定义的打印格式字符串
 */

//DDLogLevelVerbose
- (NSString *)formatLogMessage:(DDLogMessage *)logMessage{
    
    NSString *logLevel = nil;
    switch (logMessage->_flag)
    {
        case DDLogFlagError:
            logLevel = @"[ERROR] >  ";
            break;
        case DDLogFlagWarning:
            logLevel = @"[WARN]  >  ";
            break;
        case DDLogFlagInfo:
            logLevel = @"[INFO]  >  ";
            break;
        case DDLogFlagDebug:
            logLevel = @"[DEBUG] >  ";
            break;
        default:
            logLevel = @"[VBOSE] >  ";
            break;
    }
    
    NSString *formatStr = [NSString stringWithFormat:@"%@[%@ %@][line %lu] %@",
                           logLevel, logMessage.fileName, logMessage->_function,
                           (unsigned long)logMessage->_line
                           , logMessage->_message];
    return formatStr;
}

@end
